package com.dotstone.chipism.listener;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.hardware.ConsumerIrManager;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.Util;
import com.tiqiaa.ircode.IInfraredFetcher;
import com.tiqiaa.ircode.impl.InfraredFetcher;
import com.tiqiaa.irdnasdk.IrDnaSdkHelper;
//import com.tiqiaa.irdnasdk.IrDnaSdkHelper.IrData;
import com.tiqiaa.local.LocalIrDb;
import com.tiqiaa.remote.entity.AirRemoteState;
import com.tiqiaa.remote.entity.Infrared;
import com.tiqiaa.remote.entity.Key;
import com.tiqiaa.remote.entity.Remote;

/**测试实现红外发送接口
 * */
public class InfraredSeneder implements IInfraredSeneder{

	IInfraredFetcher mFetcher;
	IInfraredDevice mDevice;
	Context ct;
	public InfraredSeneder(Context mContext){
		mFetcher = new InfraredFetcher(mContext);
		mDevice = new InfraredDevice();

		ct = mContext;
	}
	@Override
	public void send(Remote remote, Key key){
		if(remote==null||remote.getId()==null||key==null){
			return;
		}

		List<Infrared> infrareds = null;
		
//		infrareds = mFetcher.fetchAirInfrareds(remote, key, mFetcher.getAirRemoteStatus(remote)); 
//		AirRemoteState state = mFetcher.getAirRemoteStatus(remote);
//		byte[] pack = PackAirUtils.packAirState(state);
		
//		infrareds = mFetcher.fetchInfrareds(remote, key);
		infrareds = new InfraredFetcher(ct).fetchInfrareds(remote, key);
		if(infrareds!=null){
			for(Infrared ir:infrareds){
				if(ir!=null&&ir.getData()!=null){
					int freq = ir.getFreq();
					//Required ！ 必须调用，此接口将对信号格式进行验证，返回明码信号数据
					byte[] data = IrDnaSdkHelper.getIrCode(ir.getFreq(), ir.getData());
					byte[] airData = IrDnaSdkHelper.getAirVMCode(key.getProtocol());
					String str = Util.bytesToHexString(data).toUpperCase();
					String cmd = ConvertUtil.getInstance().convertInfraredCmd(str);
//					String cmd = getInfraRedCmd(data);
					//data格式详见《信号数据格式说明》文档，不同的芯片需要转换成对应的芯片目标码
					SocketManager.getInstance().sMap.get(DeviceManager.getInstance().getmWlanId()).sendMqttData(cmd);
					if(data!=null){
						//测试
//						Toast.makeText(ct, "发送信号数据长度为："+data.length, Toast.LENGTH_LONG).show();
						mDevice.sendIr(freq, data);
						
						//多组信号发送时暂停一点时间，以避免信号连续发送而电器识别错误
						try{
							Thread.sleep(50);
						}catch (InterruptedException e){
							e.printStackTrace();
						}
					}
				}
			}
		}
	}
	
	
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
	
	@Override
	public void sendAirTest(Remote remote, Key key) {
		if(remote==null||remote.getId()==null||key==null)
		{
			return;
		}

		List<Infrared> infrareds = null;
		
		infrareds = mFetcher.fetchAirTestInfrareds(remote, key); 
		
		if(infrareds!=null){
			for(Infrared ir:infrareds){
				if(ir!=null&&ir.getData()!=null){
					int freq = ir.getFreq();
					//Required ！ 必须调用，此接口将对信号格式进行验证，返回明码信号数据
					byte[] data = IrDnaSdkHelper.getIrCode(ir.getFreq(), ir.getData());
					
					//data格式详见《信号数据格式说明》文档，不同的芯片需要转换成对应的芯片目标码
					Log.w(TAG, "send....###..解析出的红外数据..data = "+JSON.toJSONString(data));
					if(data!=null){
						//测试
						Toast.makeText(ct, "发送信号数据长度为："+data.length, Toast.LENGTH_LONG).show();
						mDevice.sendIr(freq, data);
						
						//多组信号发送时暂停一点时间，以避免信号连续发送而电器识别错误
						try{
							Thread.sleep(50);
						}
						catch (InterruptedException e){
							e.printStackTrace();
						}
					}
				}
			}
		}
		
	}
}
